import { Toast } from 'vant';

export default {
  data() {
    return {
      userInfo: null,
      orderList: [
        {id: 1, text: '待付款', icon: require('@/assets/images/icon/order-pay.png'), status: '1'},
        {id: 2, text: '待发货', icon: require('@/assets/images/icon/order-send.png'), status: '2'},
        {id: 3, text: '待收货', icon: require('@/assets/images/icon/order-received.png'), status: '3'},
        // {id: 4, text: '待评价', icon: require('@/assets/images/icon/order-evaluate.png'), status: '4'},
        {id: 5, text: '退款售后', icon: require('@/assets/images/icon/order-refund.png'), status: '5'}
      ],
      serviceList: [
        {id: 1, text: '优惠券', icon: require('@/assets/images/icon/service-coupon.png'), link: 'couponStore'},
        {id: 2, text: '足迹', icon: require('@/assets/images/icon/service-footer.png'), link: 'footprint'},
        {id: 3, text: '收藏', icon: require('@/assets/images/icon/service-favorites.png'), link: 'collection'},
        {id: 4, text: '地址管理', icon: require('@/assets/images/icon/service-address.png'), link: 'address'},
        {id: 5, text: '积分商城', icon: require('@/assets/images/icon/service-shop.png'), link: 'newDiy'},
        {id: 6, text: '我的积分', icon: require('@/assets/images/icon/service-integral.png'), link: 'integral'},
        {id: 7, text: '完善资料', icon: require('@/assets/images/icon/service-data.png'), link: 'userinfo'},
        {id: 8, text: '账号设置', icon: require('@/assets/images/icon/service-setting.png'), link: 'info'}
      ]
    };
  },
  methods: {
    /**
     * 获取页面数据
     */
    getPageData: function() {
      const that = this;
      $http.get("member.member.getUserInfo").then(
        response => {
          const { result, data, msg } = response;
          if (result == 1) {
            that.userInfo = data;
            let _orderList = that.orderList;
            if (data.order && data.order.length) {
              _orderList.forEach(order => {
                let target = null;
                if (order.status == 1) {
                  target = data.order.find(item => item.status == 0);
                } else if(order.status == 2) {
                  target = data.order.find(item => item.status == 1);
                } else if(order.status == 3) {
                  target = data.order.find(item => item.status == 2);
                } else if(order.status == 5) {
                  target = data.order.find(item => item.status == 11);
                }
                order.total = target ? target.total : 0;
              });
            }
            that.orderList.splice(0, that.orderList.length, ..._orderList);
          } else {
            Toast(msg || '获取页面数据失败！');
          }
        }
      );
    },

    /**
     * 去修改昵称/头像
     */
    toEditAvatar(){
      this.$router.push(this.fun.getUrl("userinfo", {}, {pageType: 'UserInformation'}));
    },

    /**
     * 购买vip
     */
    buyVip: function() {
      this.$router.push(this.fun.getUrl("CardOpenMembership"));
    },

    /**
     * 全部订单
     */
    gotoAllOrderList: function() {
      this.$router.push(this.fun.getUrl("orderlist", {status: 0}));
    },

    /**
     * 订单跳转
     * @param {Number} status 状态
     */
    gotoOrderList: function(status) {
      if (status == 5) {
        this.$router.push(this.fun.getUrl("aftersaleslist"));
      } else {
        this.$router.push(this.fun.getUrl("orderlist", {status}));
      }
    },

    /**
     * 服务功能跳转
     * @param {Object} item 服务项
     */
    serviceClick: function(item) {
      const { id, link } = item;
      let param = {};
      if (id == 5) {
        param.page_id = 4;
      } else if (id == 7) {
        param.pageType = 'UserInformation';
      }
      this.$router.push(this.fun.getUrl(link, {}, {...param}));
    }
  },
  mounted() {
    this.getPageData();
  }
};